/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.util;

import java.io.Serializable;
import java.net.URLEncoder;
import java.util.Enumeration;
import javax.faces.FactoryFinder;
import javax.faces.application.Application;
import javax.faces.application.ApplicationFactory;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.context.FacesContextFactory;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.lifecycle.LifecycleFactory;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Nothing4ever
 */
public class UtilCommon implements Serializable {

    private ExternalContext getExternalContext() {
        return getFacesContext().getExternalContext();
    }

    private FacesContext getFacesContext() {
        // Get current FacesContext.
        FacesContext facesContext = FacesContext.getCurrentInstance();

        
        return facesContext;
    }

    private static class CommonUtilsHolder {

        static UtilCommon instance = new UtilCommon();
    }
    private static final long serialVersionUID = 1L;
    private static final String MESSAGE_CONTROL_ID = null;

    private UtilCommon() {
    }

    public static UtilCommon getInstance() {
        return CommonUtilsHolder.instance;
    }

    public Object findBean(String beanName) {
        FacesContext context = getFacesContext();
        if (context == null) {
            return null;
        }
        return context.getApplication().evaluateExpressionGet(context, "#{" + beanName + "}", Object.class);
    }

    public void redirectPage(String url) {
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getRequestParameter(String name) {
        return getExternalContext().getRequestParameterMap().get(name);
    }

    public String getRequestUri() {
        return ((HttpServletRequest) getExternalContext().getRequest()).getRequestURI();
    }

    public String getRequestUrl() {
        Enumeration<String> lParameters;
        String sParameter;
        StringBuilder sbURL = new StringBuilder();
        Object oRequest = FacesContext.getCurrentInstance().getExternalContext().getRequest();

        try {
            if (oRequest instanceof HttpServletRequest) {
                sbURL.append(((HttpServletRequest) oRequest).getRequestURL().toString());

                lParameters = ((HttpServletRequest) oRequest).getParameterNames();

                if (lParameters.hasMoreElements()) {
                    if (!sbURL.toString().contains("?")) {
                        sbURL.append("?");
                    } else {
                        sbURL.append("&");
                    }
                }

                while (lParameters.hasMoreElements()) {
                    sParameter = lParameters.nextElement();

                    sbURL.append(sParameter);
                    sbURL.append("=");
                    sbURL.append(URLEncoder.encode(((HttpServletRequest) oRequest).getParameter(sParameter), "UTF-8"));

                    if (lParameters.hasMoreElements()) {
                        sbURL.append("&");
                    }
                }
            }
        } catch (Exception e) {
            // Do nothing
        }

        return sbURL.toString();
    }

    public Object getSessionBean(String varSession) {
        //varSession="#{textDetailBean}";
        ApplicationFactory appFactory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
        Application app = appFactory.getApplication();
        return (app.createValueBinding(varSession).getValue(getFacesContext()));
    }

    public void setSessionMapValue(String key, Object value) {
        getExternalContext().getSessionMap().put(key, value);
    }

    public Object getSessionMapValue(String key) {
        return getExternalContext().getSessionMap().get(key);
    }

    public void removeAllSessionMapValue() {
        getExternalContext().getSessionMap().clear();
    }

    public void showInformationMessage(String clientId, String summary, String detail) {
        getFacesContext().addMessage(clientId, new FacesMessage(FacesMessage.SEVERITY_INFO, summary, detail));
    }

    public void showErrorsMessage(String clientId, String summary, String detail) {
        getFacesContext().addMessage(clientId, new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, detail));
    }

    public void showInformationMessage(String summary, String detail) {
        showInformationMessage(MESSAGE_CONTROL_ID, summary, detail);
    }

    public void showErrorsMessage(String summary, String detail) {
        showErrorsMessage(MESSAGE_CONTROL_ID, summary, detail);
    }

    public void showInformationMessage(String message) {
        showInformationMessage(message, message);
    }

    public void showErrorsMessage(String message) {
        showErrorsMessage(message, message);
    }

    public String getContextParam(String name) {
        return getExternalContext().getInitParameter(name);
    }
}
